// Copyright (C) 2013 OpenPlayHuman Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
 * @fileoverview ... TODO 
 * @author openplayhuman@gmail.com
 * 
 */
function  openplay_key(ss_name) {
	var openplay_name2key = {"OpenPlay Activity":"0Ag7FmV-UPpmpdFNqREwzLVd3UldFZmF1TkFUcExUOWc"};
	return openplay_name2key[ss_name];
}
function  openplay_show_ss(ss_name) { //Debug
    	var googleSpreadsheet;
    	var url, sample_url, num_cols, num_rows;
    	var ss_key = openplay_key(ss_name);
    	url = "https://spreadsheets.google.com/pub?key=" + ss_key + "&hl=en&output=html";
	    sample_url = url;
    	localStorage.clear();
	    googleSpreadsheet = new GoogleSpreadsheet();
	    googleSpreadsheet.url(url);
	    googleSpreadsheet.load(function(result) {
	    	num_cols = result.data[0];
	    	if (num_cols != 0)
	    		num_rows = (result.data.length - 1)/num_cols -1;
	      $('#results').html("NUM_COLS:" + num_cols + "\nNUM_DATA_ROWS:" + num_rows + "\nDATA:" + JSON.stringify(result.data));
	      //$('#results').html(JSON.stringify(result.data).replace(/,/g,"\n"));
	    });

 };

 function  openplay_get_sscol(ss_name,attribute) {
 	var googleSpreadsheet;
 	var url;
 	var ss_key = openplay_key(ss_name);
 	var indexAttributeCol;

 	url = "https://spreadsheets.google.com/pub?key=" + ss_key + "&hl=en&output=html";
	    sample_url = url;
 	    localStorage.clear();
	    googleSpreadsheet = new GoogleSpreadsheet();
	    googleSpreadsheet.url(url);
	    
	    googleSpreadsheet.load(function(result) {
	    	num_cols = parseInt(result.data[0]);
	    	if (num_cols != 0)
	    		num_rows = (result.data.length - 1)/num_cols -1;
	    	
	    	
	    	//get attribute column index from name, start after "# of columns" cell A0
	    	for (var i=1;i<num_cols+1;i++){	
	    		if (attribute == result.data[i])
	    			indexAttributeCol = i;
	    	}
	    	var offset = num_cols+indexAttributeCol;
	    

	    	for(var i=offset; i<result.data.length; i+=num_cols)
	    	     document.getElementById(attribute).options[(i-offset)/num_cols]=new Option(result.data[i],result.data[i]);
	    	  
	      
	    });
	
};

function  openplay_build_selection (attribute,attribute_explain) {

	var formLocation = document.getElementById("form1");
	var listExplain = document.createElement("l");
	listExplain.setAttribute("id",("tag"+attribute));
	formLocation.appendChild(listExplain);
	document.getElementById("tag"+attribute).innerHTML=attribute_explain;
	var listColContents = document.createElement("select");
	listColContents.setAttribute("name",attribute + "New");
	listColContents.setAttribute("id",attribute);
	formLocation.appendChild(listColContents);
	var listBreak = document.createElement("br");
	formLocation.appendChild(listBreak);
	
		 
}
function openplay_get_unique(ss_name,attribute,attribute_explain) {
 	var googleSpreadsheet;
 	var url;
 	var ss_key = openplay_key(ss_name);
 	var indexAttributeCol =0;
 	var list=[];
 	openplay_build_selection(attribute,attribute_explain);
 	url = "https://spreadsheets.google.com/pub?key=" + ss_key + "&hl=en&output=html";
	    sample_url = url;
 	    localStorage.clear();
	    googleSpreadsheet = new GoogleSpreadsheet();
	    googleSpreadsheet.url(url);
	    
	    googleSpreadsheet.load(function(result) {
	    	num_cols = parseInt(result.data[0]);
	    	if (num_cols != 0)
	    		num_rows = (result.data.length - 1)/num_cols -1;
	    	
	    	
	    	//get attribute column index from name, start after "# of columns" cell A0
	    	for (var i=1;i<num_cols+1;i++){	
	    		if (attribute == result.data[i])
	    			indexAttributeCol = i;
	    	}
	    	var offset = num_cols+indexAttributeCol;
	    	var c=0;
	    	for(var i=offset; i<result.data.length; i+=num_cols){
	    	      list[c]=result.data[i];
	    	      c++;
	    	}
	    	ulist= new get_unique_list(list);
	    	for(var i=0; i<ulist.length; i++)
	    		document.getElementById(attribute).options[i]=new Option(ulist[i],ulist[i]);
	    	  
	    });
};

function get_unique_list(list) {
	   var u = {}, a = [];
	   for(var i = 0, l = list.length; i < l; ++i){
	      if(u.hasOwnProperty(list[i])) {
	         continue;
	      }
	      a.push(list[i]);
	      u[list[i]] = 1;
	   }
	   return a;
	
	
};

    

